<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <link rel="stylesheet" href="css/theme.css">
    <style>
        body {
            background-image: url(img/login_bg.jpg);
            background-size: 100%;
            display: flex;
            height: 100vh;
            background-attachment: fixed;
        }

        main {
            background-color: white;
            border-radius: 5px;
            max-width: 500px;
            margin: auto;
            width: 100%;
            padding: 30px 20px;
        }

        input {
            display: block;
            width: 100%;
        }

        h2 {
            text-align: center;
        }

        .login_form {
            display: block;
        }

        .register_form {
            display: none;
        }

        .login_info_form {
            display: none;
        }

        .form_change {
            display: flex;
            width: 100%;
            margin-top: 20px;
            margin-bottom: 20px;
            justify-content: space-around;
        }

        .form_change span {
            padding-bottom: 5px;
            cursor: pointer;
            color: var(--description_color);
        }

        .form_change span.active {
            color: var(--theme_color);
            border-bottom: 2px solid var(--theme_color);
        }

        input {
            height: 40px;
            border-radius: 5px;
            margin: 20px auto;
            padding: 0 10px;
            color: #606266;
            border: 1px solid var(--border_color);
        }
        .reset_a{
            float: right;
            display: block;
            margin: 10px auto;
            color: var(--theme_color);
            text-decoration: none;
        }
        button {
            display: block;
            width: 100%;
            font-size: 16px;
            color: white;
            height: 44px;
            border-radius: 5px;
            border: none;
            background-color: var(--theme_color);
        }

        @media (max-width: 768px) {
            body {
                padding: 0 20px;
            }
        }

        .avatar_img {
            width: 100px;
            height: 100px;
            display: flex;
            border-radius: 50%;
            margin: auto;
        }

        .nickname {
            text-align: center;
            margin-bottom: 30px;
            margin-top: 10px;
            font-size: 18px;
        }

        .a_btn {
            padding: 10px 20px;
            color: white;
            border-radius: 5px;
            margin: 0 5px;
            line-height: 20px;
            text-decoration: none;
        }

        .btn_flex {
            display: flex;
            width: 100%;
            justify-content: center;
        }

        .btn_flex .a_btn:nth-child(1) {
            background-color: var(--theme_color);
        }

        .btn_flex .a_btn:nth-child(2) {
            background-color: rgb(165, 226, 74);
        }

        .btn_flex .a_btn:nth-child(3) {
            background-color: rgb(235, 80, 53);
        }

        @media (max-width: 500px) {
            .a_btn {
                padding: 10px 10px;
            }
        }

        .mark_bg {
            display: none;
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .notice_main {
            max-width: 500px;
            height: auto;
            margin: 50px auto auto auto;
            background-color: white;
            border-radius: 5px;
            padding: 30px 20px;
        }
    </style>
</head>

<body>
<main>
    <div>
        <h2 id="stats">用户登录</h2>
        <div class="form_change" id="form_change">
            <span class="active" id="change_login">用户登录</span>
            <span id="change_register">用户注册</span>
        </div>
        <div class="login_form active" id="login_form">
            <input type="text" placeholder="请输入账号" id="user_id" value="lxq">
            <input type="password" placeholder="请输入密码" name="" id="user_password" value="24166">
            <a class="reset_a" href="reset_password.html" target="_blank">忘记密码</a>
            <button id="to_login">登录</button>
        </div>
        <div class="register_form" id="register_form">
            <input type="text" placeholder="请输入昵称" id="set_username">
            <input type="password" placeholder="请设置密码" id="set_password">
            <button id="to_register">注册</button>
        </div>
        <div class="login_info_form" id="login_info_form">
            <img class="avatar_img" src="img/default_avatar.webp" alt="">
            <p class="nickname" id="nickname">Excellent</p>
            <div class="btn_flex">
                <a href="index.html" class="a_btn">回到首页</a>
                <a href="user/user_zone.html" class="a_btn">个人空间</a>
                <a href="/logout" id="logout" class="a_btn">退出登录</a>
            </div>
        </div>
    </div>
</main>
<div class="mark_bg">
    <div class="notice_main">
        <h2>登录成功</h2>
        <p>你的账号：12345679；你的密码：467979；昵称：Excel。是否现在登录</p>
        <div class="btn_flex">
            <button class="a_btn">回到首页</button>
            <button class="a_btn">个人空间</button>
            <button class="a_btn">退出登录</button>
        </div>
    </div>
</div>
<script>
    /*fetch('/is_login', {method: 'GET'}).then(res => {
        res.json().then(data => {
            //console.log(data)
            if (data.isLogin) {
                document.getElementById('stats').style.display = 'none'
                document.getElementById('login_form').style.display = 'none'
                document.getElementById('form_change').style.display = 'none'
                document.getElementById('nickname').textContent = data.userName
                document.getElementById('login_info_form').style.display = 'block'
            }
        })
    }).catch(err => {
        console.log(err)
    })*/

    document.getElementById('to_register').addEventListener('click', () => {
        let inputUserName = document.getElementById('set_username').value
        let inputPassword = document.getElementById('set_password').value
        if (inputUserName == '' || inputPassword == '') {
            alert('请先把内容填写完整')
            return
        }
        let postData = {
            userName: inputUserName, password: inputPassword
        }
        fetch('/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(postData)
        }).then(response => {
            response.json().then(data => {
                let respText = ''
                if (data.result) {
                    respText = '登录成功！<br>你的账号：' + data.userId
                } else {
                    respText = '登录失败：' + data.because
                }
                document.write(respText)
            })
        }).catch(err => {
            alert('注册失败：' + err)
        })
    })

    document.getElementById('change_login').addEventListener('click', function () {
        document.getElementById('change_login').classList.add('active')
        document.getElementById('change_register').classList.remove('active')
        document.getElementById('login_form').style.display = 'block'
        document.getElementById('register_form').style.display = 'none'
        document.getElementById('stats').textContent = '用户登录'
    })
    document.getElementById('change_register').addEventListener('click', function () {
        document.getElementById('change_register').classList.add('active')
        document.getElementById('change_login').classList.remove('active')
        document.getElementById('login_form').style.display = 'none'
        document.getElementById('stats').textContent = '用户注册'
        document.getElementById('register_form').style.display = 'block'
    })

    document.getElementById('to_login').addEventListener('click', () => {
        /*let paramter = {
            userId: document.getElementById('user_id').value, password: document.getElementById('user_password').value
        }
        console.log(paramter)*/
        let postData = 'username=' + document.getElementById('user_id').value + '&password=' + document.getElementById('user_password').value
        fetch('/login', {
            method: 'POST', headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }, body: postData
        }).then(res => {
            res.json().then(data => {
                if (data.code) {
                    window.location.replace(data.redirectUrl)
                } else {
                    alert('登录失败')
                }
                //
                console.log(data)
            })
        }).catch(err => {
            console.log(err)
        })
    })
</script>
</body>

</html>